//
//  AISoulmateController.m
//  Mia
//
//  Created by Vill on 2024/1/11.
//

#import "AISoulmateController.h"
#import "AIChooseSoulmateView.h"     // 选择人物模型
#import "AIInfoSetView.h"            // 修改人物信息
#import "AIInterstedView.h"          // 兴趣标签
#import "AIVipPlusController.h"      // VIP界面

@interface AISoulmateController ()

@property (nonatomic, strong) AIChooseSoulmateView *mateView;
@property (nonatomic, strong) AIInfoSetView *infoSetView;
@property (nonatomic, strong) AIInterstedView *interstedView;  //兴趣标签
@property (nonatomic, copy) NSString *chatAiId;    //选择的心理咨询ai

@property (nonatomic, strong) NSMutableDictionary *nextParams;  //选择的信息参数

@end

@implementation AISoulmateController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 更改背景
    [AIUnityInstance.instance.ufw sendMessageToGOWithName:[@"IOSBridge" UTF8String] functionName:[@"ChangeBG" UTF8String] message:[@"3" UTF8String]];
    [self.view addSubview:self.mateView];
}

#pragma mark ---------------- lazy ------------------
// 模型View
- (AIChooseSoulmateView *)mateView{
    if(!_mateView){
        _mateView = [[AIChooseSoulmateView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
        WeakSelf(self);
        _mateView.itemClickBlock = ^(AISoulMateModel * _Nonnull model, NSInteger index) {
            [weakself chooseItem:model index:index];
        };
        _mateView.nextClickBlock = ^{
            // 近距离
            [AIUnityInstance.instance.ufw sendMessageToGOWithName:[@"IOSBridge" UTF8String] functionName:[@"NearPlayer" UTF8String] message:[@"" UTF8String]];
            [weakself creatInfoView];
            [weakself.mateView removeFromSuperview];
            weakself.mateView = nil;
        };
        // 充值vip
        _mateView.needVipBlock = ^(AISoulMateModel * _Nonnull model, NSInteger index) {
            weakself.chatAiId = model.ID;
            AIVipPlusController *vc = [[AIVipPlusController alloc] init];
            vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
            [weakself presentViewController:vc animated:YES completion:nil];
        };
    }
    return _mateView;
}

// 设置信息View
- (AIInfoSetView *)infoSetView{
    if(!_infoSetView){
        _infoSetView = [[AIInfoSetView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
        WeakSelf(self);
        // 关闭页面
        _infoSetView.closeClickBlock = ^{
            [AIUnityInstance.instance.ufw sendMessageToGOWithName:[@"IOSBridge" UTF8String] functionName:[@"FarPlayer" UTF8String] message:[@"" UTF8String]];
            [UIView animateWithDuration:0.3 animations:^{
                weakself.mateView.alpha = 1;
            } completion:^(BOOL finished) {
                [weakself.view addSubview:weakself.mateView];
                [weakself.infoSetView removeFromSuperview];
                weakself.infoSetView = nil;
            }];
        };
        // 下一步点击
        _infoSetView.nextActionBlock = ^(NSMutableDictionary * _Nonnull dic) {
            weakself.nextParams = dic;
            [AIUnityInstance.instance.ufw sendMessageToGOWithName:[@"IOSBridge" UTF8String] functionName:[@"FarPlayer" UTF8String] message:[@"" UTF8String]];
            [weakself creatInterstedView];
        };
    }
    return _infoSetView;
}

// 兴趣标签view
- (AIInterstedView *)interstedView{
    if(!_interstedView){
        _interstedView = [[AIInterstedView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
        // 返回按钮点击
        WeakSelf(self);
        _interstedView.backActionBlock = ^{
            [UIView animateWithDuration:0.3 animations:^{
                weakself.infoSetView.alpha = 1;
            } completion:^(BOOL finished) {
                [weakself.view addSubview:weakself.infoSetView];
                [weakself.interstedView removeFromSuperview];
                weakself.interstedView = nil;
            }];
        };
        //skip按钮点击
        _interstedView.skipActionBlock = ^{
            [weakself initInfoWithDic:weakself.nextParams];
        };
        //确认按钮点击
        _interstedView.doneActionBlock = ^(NSArray * _Nonnull interestTagIds) {
            weakself.nextParams[@"interestTagIds"] = interestTagIds;
            [weakself initInfoWithDic:weakself.nextParams];
        };
    }
    return _interstedView;
}

#pragma mark --------------- chooseitem ----------------
- (void)chooseItem:(AISoulMateModel *)model index:(NSInteger)index{
    NSLog(@"chooseindex------- %ld",index);
    self.chatAiId = model.ID;
//    NSString *indexStr = [NSString stringWithFormat:@"%ld",(long)index];
//    [AIUnityInstance.instance.ufw sendMessageToGOWithName:[@"IOSBridge" UTF8String] functionName:[@"PlayerTalk" UTF8String] message:[@"你好" UTF8String]];
    [AIUnityInstance.instance.ufw sendMessageToGOWithName:[@"IOSBridge" UTF8String] functionName:[@"LoadPlayer" UTF8String] message:[model.arUrl UTF8String]];
}

// 拉起修改个人信息
-(void)creatInfoView{
    [UIView animateWithDuration:0.3 animations:^{
        self.infoSetView.alpha = 1;
    } completion:^(BOOL finished) {
        self.infoSetView.chooseAiId = self.chatAiId;
        [self.view addSubview:self.infoSetView];
    }];
}

// 拉起兴趣标签界面
-(void)creatInterstedView{
    [UIView animateWithDuration:0.3 animations:^{
        self.infoSetView.alpha = 0;
    } completion:^(BOOL finished) {
        [self.view addSubview:self.interstedView];
        [self.infoSetView removeFromSuperview];
        self.infoSetView = nil;
    }];
}

// 初始信息（下一步下一步调用）
#pragma mark -------------- 设置个人消息成功 跳转进入主页 ---------
- (void)initInfoWithDic:(NSMutableDictionary *)params{
    NSLog(@"params ----------  %@",params);
    [SVProgressHUD show];
    [AIManager requestMethod:@"POST" url:api_customInitInfo parameters:params successBlock:^(id  _Nullable result) {
        [SVProgressHUD dismiss];
        // 更新用户信息
        AIUserInfoModel *userInfo = [AIUserInfoManager shareManager].userInfo;
        userInfo.nickName = params[@"nickName"];
        userInfo.birthday = params[@"birthday"];
        userInfo.sexType = params[@"sexType"];
        userInfo.chatAiId = [params[@"chatAiId"] integerValue];
        [[AIUserInfoManager shareManager] updateUserInfo:userInfo];
        if (self.soulmateDoneBlock){
            self.soulmateDoneBlock();
        }
    } failedBlock:^(NSError * _Nullable error) {
        [SVProgressHUD dismiss];
    } showHUD:NO];
}

@end
